﻿using System;
using System.Globalization;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Customers
{
    public partial class Create : BasePage {
        public string FirstNameQry
        {
            get { return Request.QueryString["firstname"] ?? ""; }
        }
        public string SecondNameQry
        {
            get { return Request.QueryString["secondname"] ?? ""; }
        }
        public string AddressQry
        {
            get { return Request.QueryString["address"] ?? ""; }
        }
        public bool ContactableOnlyQry
        {
            get { return Request.QueryString["contactable"] != null && bool.Parse(Request.QueryString["contactable"]); }
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "")) {
                return;
            }

            FirstName.Text = FirstNameQry;
            SecondName.Text = SecondNameQry;
            Address1.Text = AddressQry;
            Contactable.Checked = ContactableOnlyQry;
            ReturnToSearchLink.NavigateUrl = RedirectToSearchPath("");

            if (Request.QueryString["id"] != null) {
                CreateNewCustomer.Text = "Create Customer and Update Enquiry";
            }
        }

        private string RedirectToSearchPath(string customerId) {
            var redirectPath = "~/Customers/Search.aspx?firstname=" + FirstNameQry +
                               "&secondname=" + SecondNameQry + "&address=" + AddressQry + "&contactable=" +
                               ContactableOnlyQry;

            if (customerId.Length > 0) {
                redirectPath += "#" + customerId;
            }

            if (Request.QueryString["id"] != null && customerId.Length > 0) {
                //sent from change customer
                
                redirectPath = "~/Enquiries/Customers/Change.aspx?id=" + Request.QueryString["id"] + "&changeto=" + customerId;
            }

            return redirectPath;
        }

        protected void CreateNewCustomerClick(object sender, EventArgs e) {
            var customer = new Customer
                {
                    Address1 = Address1.Text,
                    Address2 = Address2.Text,
                    Address3 = Address3.Text,
                    Address4 = Address4.Text,
                    Address5 = Address5.Text,
                    CanBeContactedForFeedback = Contactable.Checked,
                    Email = Email.Text,
                    Fax = Fax.Text,
                    FirstName = FirstName.Text,
                    Flagged = Flagged.Checked,
                    Phone = Phone.Text,
                    SecondName = SecondName.Text
                };

            if (!IsValidObject(customer, CreateNewCustomerResultMessage)) {
                return;
            }

            CustomerRepository.Add(customer);
            Response.Redirect(RedirectToSearchPath(customer.Id.ToString(CultureInfo.InvariantCulture)));
        }
    }
}